package com.whisper.hfdp.design.observer.weather.display;

import com.whisper.hfdp.design.observer.weather.Observer;
import com.whisper.hfdp.design.observer.weather.Subject;

public class StatisticsDisplay implements Observer, DisplayElement {

    private float minTemperature;
    private float maxTemperature;
    private float averageTemperature;

    private int changedCount = 0;
    private Subject weatherData;

    public StatisticsDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    @Override
    public void update(float temperature, float humidity, float pressure) {
        if (minTemperature == 0) {
            minTemperature = temperature;
        } else {
            minTemperature = Math.min(temperature, minTemperature);
        }
        maxTemperature = Math.max(temperature, maxTemperature);
        averageTemperature = (averageTemperature * changedCount + temperature) / ++changedCount;
        display();
    }

    @Override
    public void display() {
        System.out.println("temperature: min=" + minTemperature + ", max=" + maxTemperature + ", average=" + averageTemperature);
    }
}
